import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator

def load_data(file_path):
    """
    加载电力消耗和生产数据集
    
    参数:
        file_path: 数据集文件路径
        
    返回:
        加载的数据集DataFrame
    """
    df = pd.read_csv(file_path)
    # 将DateTime列转换为datetime类型
    df['DateTime'] = pd.to_datetime(df['DateTime'])
    return df

def prepare_time_series_data(df, target_column, sequence_length=24, batch_size=32, train_split=0.8):
    """
    准备时间序列数据用于RNN/LSTM模型训练
    
    参数:
        df: 输入数据DataFrame
        target_column: 目标列名称
        sequence_length: 序列长度
        batch_size: 批次大小
        train_split: 训练集比例
        
    返回:
        训练生成器, 测试生成器, 缩放器, 训练数据, 测试数据
    """
    # 提取目标列数据
    data = df[target_column].values.reshape(-1, 1)
    
    # 数据归一化
    scaler = MinMaxScaler()
    data_scaled = scaler.fit_transform(data)
    
    # 划分训练集和测试集
    train_size = int(len(data_scaled) * train_split)
    train_data = data_scaled[:train_size]
    test_data = data_scaled[train_size:]
    
    # 创建时间序列生成器
    train_generator = TimeseriesGenerator(
        train_data, train_data, 
        length=sequence_length, 
        batch_size=batch_size
    )
    
    test_generator = TimeseriesGenerator(
        test_data, test_data, 
        length=sequence_length, 
        batch_size=batch_size
    )
    
    return train_generator, test_generator, scaler, train_data, test_data